import 'dart:math';
import 'package:flutter/material.dart';
import 'dart:async';

class Carousel extends StatefulWidget {
  final List Imgs;
  final Radius borderRadius;
  Carousel({Key key, this.Imgs,this.borderRadius}) : assert(Imgs != null);
  @override
  State createState() => new CarouselState();
}

class CarouselState extends State<Carousel> {
  PageController _pageController ;
  double _currentPage = 0.0;
  int pageIndex = 0;
  List listImg = [];
  List<Color> potColors=[];
  Timer time=null;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    this.listImg = widget.Imgs;
    for(var index=0;index<this.listImg.length;index++){
        this.potColors.add(Colors.white);
    }

    _pageController= new PageController();
    time= Timer.periodic(Duration(milliseconds: 2500), (time) {
        if(_pageController.page==null){

        }else{
          if (_pageController.page.toInt() == (listImg.length-1)) {
            _pageController.animateToPage(
              0,
              duration: Duration(milliseconds: 1000),
              curve: Curves.easeInOut,
            );
          } else {
            _pageController.nextPage(
                duration: Duration(milliseconds: 1000), curve: Curves.easeInOut);
          }
        }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _pageController.dispose();
    time=null;
    super.dispose();

  }

  //页面
  page({index: 0, parallaxOffset: 0.0, width: 0.0}) {
    return new Center(
        child: new Column(
      children: <Widget>[
        new Container(
          height: 200.0,
          width: width,
          child: new Stack(
            children: <Widget>[
              new Positioned(
                child: new Container(
                  height: 30.0,
                  padding: EdgeInsets.only(left: 5.0),
                  width: width,
                  child: new Row(
                    children: <Widget>[
                      new Expanded(
                        flex: 1,
                        child: new Transform(
                          transform: new Matrix4.translationValues(
                              parallaxOffset, 0.0, 0.0),
                          child: new Text(
                            listImg[index]['title'],
                            style: new TextStyle(
                              color: Colors.white,
                            ),
                            maxLines: 1,
                          ),
                        ),
                      ),
                    ],
                  ),
                  decoration: new BoxDecoration(
                    color: Colors.black.withOpacity(0.5),
                    borderRadius: new BorderRadius.only(bottomLeft:widget.borderRadius,bottomRight: widget.borderRadius)
                  ),
                ),
                bottom: 0.0,
              )
            ],
          ),
          decoration: new BoxDecoration(
            borderRadius: new BorderRadius.all(widget.borderRadius),
              image: new DecorationImage(
                  image: new NetworkImage(
                    listImg[index]['img'],
                  ),
                  fit: BoxFit.fill)),
        ),
      ],
    ));
  }

  @override
  Widget build(BuildContext context) {
    return new Stack(
      children: <Widget>[
        new SizedBox(
          height: 200.0,
          child: new LayoutBuilder(
              builder: (context, constraints) => new NotificationListener(
                    onNotification: (ScrollNotification note) {
                      setState(() {
                        _currentPage = _pageController.page;
                      });
                    },
                    child: new PageView.custom(
                      pageSnapping: false,
//                  physics: const PageScrollPhysics(parent: const BouncingScrollPhysics()),
                      controller: _pageController,
                      onPageChanged: (i) {
                        this.pageIndex = i;
                      },
                      childrenDelegate: new SliverChildBuilderDelegate(
                        //生成widget
                        (context, index) {
//                          print(constraints.maxWidth);
                          return page(
                              index: index,
                              parallaxOffset: constraints.maxWidth /
                                  1.0 *
                                  (index - _currentPage),
                              width: constraints.maxWidth);
                        },
                        childCount: listImg.length, //个数
                      ),
                    ),
                  )),
        ),
        new Positioned(
          bottom: 0.0,
          right: 0.0,
          height: 30.0,
          child: new PointPage(
            controller: _pageController,
            itemCount: listImg.length,
            pageIndex: pageIndex,
            borderRadius: widget.borderRadius,
          )
        )
      ],
    );
  }
}

class PointPage extends AnimatedWidget{
  final PageController controller;
  final int itemCount;
  int pageIndex;
  Radius borderRadius;
  PointPage({
    this.controller,
    this.itemCount,
    this.pageIndex,
    this.borderRadius,
  }):super(listenable:controller);

   _buildPoint(int index){
//    print(pageIndex);
    return new List<Widget>.generate(index, (i){
      var  colors=Colors.white;
      if(pageIndex==i){
        colors=Colors.black;
      }
      return Row(
        children: <Widget>[
          new Material(
            borderRadius: BorderRadius.circular(10.0),
            child: new Container(
              width: 10.0,
              height: 10.0,
              color: colors,
            ),
          ),
          new SizedBox(
            width: 10.0,
          ),
        ],
      );
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Row(
      children: _buildPoint(itemCount),
    );
  }


}